﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FDEE.Model
{
    /// <summary>
    /// T梁的等宽现浇桥面
    /// </summary>
    public class SDeckSlab : MemberBase
    {
        public int Wmin, Wmax;
        public int Length;
        public int Cover = 40;
        /// <summary>
        /// T梁现浇桥面板
        /// </summary>
        /// <param name="typeName"></param>
        /// <param name="pierNumber"></param>
        /// <param name="width1">板边缘至隔板中线距离</param>
        /// <param name="width2">板边缘至隔板中线距离</param>
        /// <param name="length">梁间距</param>
        public SDeckSlab(string typeName, string pierNumber, int width1, int width2, int length)
            : base(typeName, pierNumber)
        {
            Wmax = Math.Max(width1, width2);
            Wmin = Math.Min(width1, width2);
            Length = length;
        }

        public override void SetConcreteClass()
        {
            ConcreteClass = eConcreteClass.C45;
        }

        public override void SetConcreteVolume()
        {
            ConcreteVolumn = 0;
        }

        public override void SetRebarList()
        {
            #region N1
            int n11 = Wmin / 150 + 1;
            RebarList.Add(new M2ABCDRebarRecord(1, 1, "", eDB.DB16, 100,480, Length, 480, n11, eShapeType.M2ABCD));
            int n12 = (Wmax - Wmin) / 150;
            for (int i = 0; i < n12; i++)
            {
                int x = (int)Math.Round(-((double)Length) / (Wmax - Wmin) * (i + 1) * 150 + Length, 0, MidpointRounding.AwayFromZero);
                if (x < 200)
                {
                    continue;
                }
            
                if (480 > x)
                    RebarList.Add(new MABRebarRecord(1, i + 2, "", eDB.DB16, 480, x, 1, eShapeType.MAB));
                else
                    RebarList.Add(new MABRebarRecord(1, i + 2, "", eDB.DB16, 480, x, 1, eShapeType.MBA));
            }
            #endregion

            #region N2
            int n2 = (Length - 150) / 150 + 1;
            double startX = (Length - (n2 - 1) * 150) * 0.5;
            for (int i = 0; i < n2; i++)
            {
                double k = (Wmax - Wmin) / ((double)Length);
                int y = (int)Math.Round(k * (i * 150 + startX) + Wmin, 0, MidpointRounding.AwayFromZero);
                RebarList.Add(new MSZRebarRecord(2, i + 1, "", eDB.DB12, 1, eShapeType.MSZ, y, 50, 500));
            }
            #endregion

            RebarList.Sort();
        }


        int[] GetSplice(int L, int cover, int lap, int tryL1)
        {
            int Lr;
            int L1 = tryL1;
            int StandardLen = 12000;
            int LengthRaw = L - cover * 2;
            int n;
            if (LengthRaw <= StandardLen)
            {
                Lr = LengthRaw;
                n = 1;
                return new int[] { L1, n, Lr };
            }
            else
            {
                // 需要切分

                int m = (LengthRaw - (L1 - lap / 2)) / (StandardLen - lap);
                Lr = LengthRaw - (L1 - lap / 2) - (StandardLen - lap) * m + lap / 2;
                if (Lr < 3 * lap)
                {
                    if (L1 > 6000)
                    {
                        return GetSplice(L, cover, lap, L1 - 3000);
                    }
                    else
                    {
                        throw new Exception("无法配置桥面纵筋：切分不合理");
                    }

                }
                n = m + 2;
                return new int[] { L1, n, Lr };
            }

        }
    }
}
